/**
 * 
 */
package org.jinion.test.dao;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.Map;

import org.jinion.database.AbstractDatabase;
import org.jinion.database.DatabaseFactory;
import org.jinion.database.model.AbstractResult;
import org.jinion.database.model.impl.TableRow;
import org.jinion.test.model.Account;
import org.jinion.test.model.User;
import org.jinion.util.BeanMap;

/**
 * @author zhlmmc
 *
 */
public class AccountDAO {
	private AbstractDatabase myDatabase;
	private UserDAO userDAO;
	
	public AccountDAO(UserDAO userDAO) throws DAOException{
		try {
			myDatabase = DatabaseFactory.getDatabase();
		} catch (Exception e) {
			throw new DAOException(e);
		}
		
		this.userDAO = userDAO;
	}
	
	public void createAccount(Account account) throws DAOException{
		Map map = new BeanMap(account);
		// BeanMap can not handle foreign key (reference to other bean). 
		// The value of "owner" in the map is wrong.
		// We have to fix it manually.
		map.put("owner", account.getOwner().getUserId());
		
		try {
			myDatabase.insert("account", map);
		} catch (SQLException e) {
			throw new DAOException(e);
		}
	}
	
	public Account getAccount(String accountNumber) throws DAOException{
		myDatabase.where("accountNumber", accountNumber);
		try {
			AbstractResult result = myDatabase.query("account");
			if (result.size() < 1) {
				return null;
			}
			TableRow row = result.getTable().getRow(1);
			BeanMap map = new BeanMap(row.getMap());
			Account account = new Account();
			map.fillBean(account);
			// BeanMap can not handle foreign key (reference to other bean). 
			// The property "owner" is not set.
			// We have to set the properties manually.
			account.setOwner(userDAO.getUser(row.getInt("owner")));
			
			return account;
		} catch (SQLException e) {
			throw new DAOException(e);
		}
	}
	
	public Account[] getAccounts(User user) throws DAOException{
		myDatabase.where("owner", user.getUserId());
		try {
			AbstractResult result = myDatabase.query("account");
			Account[] accounts = new Account[result.size()];
			int index = 0;
			for (Iterator iterator = result.getTable().getRows().iterator(); iterator.hasNext();) {
				TableRow row = (TableRow) iterator.next();
				accounts[index] = new Account();
				BeanMap map = new BeanMap(row.getMap());
				map.fillBean(accounts[index]);
				accounts[index].setOwner(userDAO.getUser(row.getInt("owner")));
				index++;
			}
			return accounts;
		} catch (SQLException e) {
			throw new DAOException(e);
		}
	}
	
	public void updateAccount(Account account) throws DAOException{
		myDatabase.where("accountNumber", account.getAccountNumber());
		Map map = new BeanMap(account);
		map.put("owner", account.getOwner().getUserId());
		try {
			myDatabase.update("account", map);
		} catch (SQLException e) {
			throw new DAOException(e);
		}
	}
	
	/**
	 * Just for test convenient.
	 */
	public void deleteAll() throws DAOException{
		try {
			myDatabase.delete("account");
		} catch (SQLException e) {
			throw new DAOException(e);
		}
	}
}
